$(function () {
    var searchRecordBtn = $("#searchRecord");
    var searchRecord = function(){
        var table = $('#recordList').DataTable({
            //"processing": true,
            //"serverSide": true,
            //"bSort": false,
            "ajax": {
                "url": "user/getVisitRecords",
                "data": function (d) {
                    d.type = $("#userType").val();
                    d.salemanId = $("#salemanId").val();
                }
            },
            "aoColumnDefs": [{
                    "targets": 0,
                    "mRender": function (data, type, row) {
                        return row.userType===null?"--":sysUserType[row.userType];
                    }
                }, {
                    "targets": 1,
                    "mRender": function (data, type, row) {
                        return '<a href="user/index/' + row.userId + '" target="_blank">' + row.loginName + '</a>';
                    }
                }, {
                    "targets": 2,
                    "mRender": function (data, type, row) {
                        return row.name===null?"--":row.name;
                    }
                }, {
                    "targets": 3,
                    "mRender": function (data, type, row) {
                        return row.mobile;
                    }
                }, {
                    "targets": 4,
                    "mRender": function (data, type, row) {
                        return row.amount;
                    }
                }, {
                    "targets": 5,
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return row.available;
                    }
                }, {
                    "targets": 6,
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        var record = "";
                        if(row.record!==null){
                            var records = new Array();
                            records = row.record.split("|");
                            var temp = new Array();
                            temp = records[records.length-2].split("^");
                            if(temp[0].length>10){
                                record = temp[0].substring(0,10)+"...";
                            }else{
                                record = temp[0];
                            }
                        }
                        
                        return "<span id='"+row.userId+"'>编辑</span><dd>"+record+"</dd>";
                    }
                }, {
                    "targets": 7,
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return row.recordTime===null?"--":$.format.date(new Date(row.recordTime), "yyyy/MM/dd HH:mm");
                    }
                }, {
                    "targets": 8,
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return row.saleman===null?"--":row.saleman;
                    }
                }],
            "order": [7, 'desc'],
            "oLanguage": chinese,
            "iDisplayLength": 10,
            "aLengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"l<"pull-right hide"Tf>r>t<"dt-panelfooter clearfix"ip>',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "fnDrawCallback": function () {
                $("#recordList_filter").hide();
                sys.btnStop(searchRecordBtn);
                $("tbody span").click(function () {
                    var userId = $(this).attr("id");
                    $("#history").empty();
                    $("#record").val("");
                    $.ajax({
                        url:"user/getRecord",
                        data: {userId:userId},
                        type: 'POST',   
                        async:false,
                        success:function(res){
                            if(res.success){
                                var data = res.model;
                                var records = new Array();
                                records = data.record.split("|");
                                var content = "";
                                for(var i = records.length-2; i >= 0; i--){
                                    var temp = new Array();
                                    temp = records[i].split("^");
                                    content += "<li>"+temp[0]+"【"+temp[1]+" 发表于 "+temp[2]+"】</li>";
                                }
                                $("#history").html(content);
                            }
                        }
                    }).fail(function () {
                        alert('材料上传失败：服务器错误');
                    });
                    $("#userId").val(userId);
                    $('#myModal').modal('show');
                });
            }
        });
        searchRecordBtn.on('click', function () {
            sys.btnSpin(searchRecordBtn);
            table.ajax.reload();
        });
    }
    searchRecord();
    initSalemans();
    $("#cancel").click(function(){
        $('#myModal').modal('hide');
    });
    $("#confirm").click(function(){
        addRecord();
    });
});
var initSalemans = function () {
    $.get("salesman/getSalesmanData", function (res) {
        var data = res.data;
        if (null !== data) {
            var content = "";
            for (var i = 0; i < data.length; i++) {
                content += "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
            }
            $("#add-salemanId").html(content);
            $("#salemanId").html("<option value=''>全部</option>"+content);
            
        }
    });
}

var addRecord = function(){
    var userId = $("#userId").val();
    var userType = $("#add-userType").val();
    var record = $("#record").val();
    var salemanId = $("#add-salemanId").val();
    var saleman = $("#add-salemanId").find("option:selected").text();
    var obj = $("#"+userId);
    $.ajax({
        url:"user/addRecord",
        data: {userId:userId,userType:userType,record:record,saleman:saleman,salemanId:salemanId},
        type: 'POST',
        success:function(res){
            if(res.success){
                if(record.length>10){
                    record = record.substring(0,10)+"...";
                }
                $(obj).next().html(record);
                $(obj).parent().parent().find("td").eq(0).html(userType);
                $(obj).parent().next().html($.format.date(new Date(), "yyyy/MM/dd HH:mm"));
                $(obj).parent().next().next().html(saleman);
                alert("回访记录添加成功");
                $('#myModal').modal('hide');
            }else{
                alert("回访记录添加失败");
            }
        }
    }).fail(function () {
        alert('服务器错误');
    });
}